package modes

import (
	"fmt"
	"time"
)

/*练习题记录*/
type TopicLog struct {
	Id       int64       `json:"tlog_id" xorm:"id"`               //练习题记录id
	UId      int64       `json:"tlog_uid" xorm:"uid"`             //学员ID
	TId      int64       `json:"tlog_tid" xorm:"tid"`             //题id
	SId      int64       `json:"tlog_sid" xorm:"sid"`             //科目id
	CId      int64       `json:"tlog_cid" xorm:"cid"`             //班型
	NId      int64       `json:"exam_nid" xorm:"nid"`             //套题ID
	ExamId   int64       `json:"tlog_exam_id" xorm:"exam_id"`     //考试记录ID
	Answer   string      `json:"tlog_answer" xorm:"answer"`       //答案
	TF       int         `json:"tlog_tf" xorm:"tf"`               //正确错误 0正确 1错误
	UpdateAt int64       `json:"tlog_update_at" xorm:"update_at"` //更新时间
	At       int64       `json:"tlog_at" xorm:"at"`               //创建时间
	Topic    []TopicLib  `json:"Topic" xorm:"-"`                  //题
	Text     []TopicText `json:"Text" xorm:"-"`                   //选项

}

func (this *TopicLog) TableName() string {
	return "topic_log"
}
func (this *TopicLog) Save() (int64, error) {
	this.At = time.Now().Unix()
	this.UpdateAt = time.Now().Unix()
	return Db(0).Insert(this)
}
func (this *TopicLog) SaveList(list []TopicLog) error {
	const BatchSize = 100

	items := []TopicLog{}
	for _, rId := range list {
		items = append(items, rId)
	}
	for n := 0; ; n++ {
		end := (n + 1) * BatchSize
		if end >= len(items) {
			end -= (end - len(items))
		}

		_, err := Db(0).Table(this).Insert(items[n*BatchSize : end])
		if err != nil {
			return err
		}

		if end >= len(items) {
			break
		}
	}
	return nil

}
func (this *TopicLog) Get() (bool, error) {
	return Db(0).Get(this)
}

func (this *TopicLog) GetLog() (bool, error) {
	where := fmt.Sprintf("uid = %d and tid = %d and nid = %d", this.UId, this.TId, this.NId)
	return Db(0).Where(where).Get(this)
}

func (this *TopicLog) delete(where string) (int64, error) {
	return Db(0).Where(where).Delete(this)
}
func (this *TopicLog) Del() (int64, error) {
	where := fmt.Sprintf("id =%d", this.Id)
	return this.delete(where)
}
func (this *TopicLog) Count(where string) (int64, error) {
	var val Record
	return Db(0).Where(where).Count(&val)
}
func (this *TopicLog) update(where string, field string) (int64, error) {
	return Db(0).Where(where).Cols(field).Update(this)
}

func (this *TopicLog) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *TopicLog) Set(field string) (int64, error) {
	where := fmt.Sprintf("uid = %d and tid = %d", this.UId, this.TId)
	return this.update(where, field)
}

func (this *TopicLog) where(fage, count, page int, where, field string) ([]TopicLog, error) {
	list := make([]TopicLog, 0)
	var err error
	if field == "" {
		field = "id"
	}
	if 0 == fage { // 从小到大排序
		err = Db(0).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(0).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}
func (this *TopicLog) List(val *Pager) error {
	val.TName = this.TableName()
	w := fmt.Sprintf("uid = %d ", this.UId)
	val.Total, _ = this.Count(w)
	list, err := this.where(val.Rules, val.Count, val.Page-1, w, "")
	if err == nil {
		val.List = list
	}
	return err
}

func (this *TopicLog) Lookups(val *Pager) error {
	list := make([]TopicLog, 0)
	val.TName = "topic"
	val.Page--
	var field, sort string
	switch val.Coll {
	case 0:
		field = fmt.Sprintf("%s.at", val.TName) // 时间排序
	default:
		field = fmt.Sprintf("%s.at", val.TName) // 状态
	}
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s asc", field)
	} else {
		sort = fmt.Sprintf("%s desc", field)
	}
	val.Total, _ = Db(0).Table(val.TName).Where(val.ToWhere()).Count(this)
	err := Db(0).Table(val.TName).
		Join("INNER", "studen_info", fmt.Sprintf("%s.uid = studen_info.id", val.TName)).
		Where(val.ToWhere()).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	return err

}

func (this *TopicLog) ExamWrong(val *Pager) error {
	list := make([]TopicLog, 0)
	val.TName = "topic_log"
	val.Page--
	var field, sort string
	field = fmt.Sprintf("%s.at", val.TName) // 时间排序
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s asc", field)
	}
	val.Total, _ = Db(0).Table(val.TName).Where(val.ToWhere()).Count(this)
	err := Db(0).Table(val.TName).
		Where(val.ToWhere()).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
		for i, row := range list {
			list[i].Topic, _ = GetTopicLib(row.TId)
			list[i].Text, _ = GetToptext(row.TId)
		}
		val.List = list
	}
	return err

}

func GetTopicLib(tid int64) ([]TopicLib, error) {
	list := make([]TopicLib, 0)
	w := fmt.Sprintf("id = %d  ", tid)
	err := Db(1).
		Where(w).
		Find(&list)
	return list, err
}
